/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.BufferedOutputStream;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import merlion.common.entity.DocFile;
import merlion.common.session.stateless.FileManagerSessionLocal;

/**
 *
 * @author Zhang Ying
 */
public class ProfileImageDisplay extends HttpServlet {
    
    private static final int DEFAULT_BUFFER_SIZE = 2097152; // 2MB
    
    @EJB
    private FileManagerSessionLocal imageBean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // picture id from request
        String picId = request.getParameter("id");
        if (picId == null || picId.equals("")){
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            System.out.println("pic id is nulll");
            return;
        }
        
        // data base query the picture
        DocFile profileImage = imageBean.getProfileImageById(picId);
        if (profileImage == null){
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            System.out.println("query db and get null");
            return;
        }
        
        // servlet response attributes
        int length = profileImage.getContents().length;
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType(profileImage.getContentType());
        response.setHeader("Content-Length", String.valueOf(length));
        response.setHeader("Content-Disposition", "inline; filename=\""+profileImage.getTitle());
        
        // streams
        BufferedOutputStream output = null;
        
        try {
            // open streams
            byte[] contents = profileImage.getContents();
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
            
            // write image contents into reponse
            if (length > 0){
                output.write(contents, 0, length);
            }
        } finally {            
            if (output != null){
                try{
                    output.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
